package com.edu.eduservice.controller;


import com.edu.commonutils.R;
import com.edu.eduservice.client.VodClient;
import com.edu.eduservice.domain.EduVideo;
import com.edu.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author ljw
 * @since 2022-09-16
 */
@Api(description = "小节接口")
//@CrossOrigin
@RestController
@RequestMapping("/eduservice/eduvideo")
public class EduVideoController {

    //注入vod方法
    @Autowired
    private VodClient vodClient;

    @Autowired
    private EduVideoService eduVideoService;

    //添加小节
    @ApiOperation("添加小节")
    @PostMapping("/save")
    @ResponseBody
    public R save(@RequestBody EduVideo eduVideo){
        boolean boo = eduVideoService.save(eduVideo);//添加
        //判断添加是否成功 封装返回数据
        R r=null;
        if(boo)
          r=R.ok();
        else
            r=R.error();

        return r;
    }
    
    //修改小节
    @ApiOperation("修改小节")
    @PutMapping("/edit")
    @ResponseBody
    public R edit(@RequestBody EduVideo eduVideo){
        boolean boo = eduVideoService.updateById(eduVideo);//修改
        //判断添加是否成功 封装返回数据
        R r=null;
        if(boo)
            r=R.ok();
        else
            r=R.error();

        return r;
    }
    
    //根据编号查询小节
    @ApiOperation("根据编号查询小节")
    @GetMapping("/byid/{id}")
    @ResponseBody
    public R byid(@PathVariable String id){
        EduVideo video = eduVideoService.getById(id);//根据编号查询
        String url = "";

        if(video.getVideoSourceId()!=null  && !StringUtils.isEmpty(video.getVideoSourceId())){
            url=  vodClient.getVideoPath(video.getVideoSourceId());
        }
        // 封装返回数据
        R r = R.ok().data("video", video).data("url",url);
        return r;
    }
    
    //删除小节
    //TODO 删除小节的时候也要把里面的视屏给删掉 后面在完善
    @ApiOperation("删除小节")
    @DeleteMapping("/del/{id}")
    @ResponseBody
    public R del(@PathVariable String id){
        EduVideo eduVideo = eduVideoService.getById(id);
        if(!StringUtils.isEmpty(eduVideo.getVideoSourceId()) && eduVideo.getVideoSourceId()!=null) {
            vodClient.del(eduVideo.getVideoSourceId());
        }
        boolean boo= eduVideoService.removeById(id);//根据编号查询
    // 封装返回数据
        R r=null;
        if(boo)
            r=R.ok();
        else
            r=R.error();
        return r;
    }

}

